/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.rainbow.file;

import org.rainbow.xml.TransformerConfigurationRuntimeException;
import org.rainbow.xml.TransformerRuntimeException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.nio.channels.FileChannel;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.rainbow.udo.UdoCommonHandler;
import org.w3c.dom.Document;
/**
 *
 * @author hironaga
 */
public class FileCommonHandler extends UdoCommonHandler{

    protected FileCommonHandler() {}

    public final String DEFAULT_ENCODING = "UTF-8";

    //File書込
    public void writeFile(File file, Document doc, String encoding )
                throws FileNotFoundException, UnsupportedEncodingException, IOException {

        TransformerFactory tff = TransformerFactory.newInstance();
        Transformer tf = null;
        try {
            tf = tff.newTransformer();
        } catch (TransformerConfigurationException ex) {
            throw new TransformerConfigurationRuntimeException(ex);
        }
        //tff.setAttribute("indent-number", 4);
        tf.setOutputProperty("encoding", encoding);
        tf.setOutputProperty("standalone","yes");
        tf.setOutputProperty("indent", "yes");
        OutputStreamWriter osw = null;
        try {
            osw = new OutputStreamWriter( new FileOutputStream(file), encoding );
            tf.transform(new DOMSource(doc), new StreamResult(osw) );
        } catch (TransformerException ex) {
            throw new TransformerRuntimeException(ex);
        }
        finally{
            osw.close();
        }
    }

    //
    public boolean createNewFile(File srcFile) throws IOException{

        if(srcFile.exists() && srcFile.isFile()){
            return false;
        }
        else{
            if(srcFile.isDirectory()){
                throw new IllegalArgumentException("File is Directory not File");
            }
            else {
                String parentPath = srcFile.getParent();
                File dir = new File(parentPath);
                if(!dir.exists()){
                    dir.mkdirs();
                }
            }
            srcFile.createNewFile();
            return true;
        }
    }

    //File Copy
    public void copyFile(File srcFile, File targetFile) throws IOException {
        FileChannel inChannel = null;
        FileChannel outChannel = null;
        try {
            outChannel = new FileOutputStream(targetFile).getChannel();
            inChannel = new FileInputStream(srcFile).getChannel();
            inChannel.transferTo(0, inChannel.size(), outChannel);
        } finally {
            if(inChannel != null){
                inChannel.close();
            }
            if(outChannel != null){
                outChannel.close();
            }
        }
    }

    //File Copy
    public void copyFile(File srcFile, File targetFile, String encoding) throws IOException {

        InputStreamReader in = new InputStreamReader(new FileInputStream(srcFile), encoding);
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(targetFile), encoding );
        int i;
        try{
            while( (i = in.read()) != -1 ){
                out.write(i);
            }
        }finally{
            in.close();
            out.close();
        }
    }

    //
    public File createTmpFile(File srcFile, String encoding) throws IOException{
        
        File tmpFile = File.createTempFile("rainbow", null, new File(srcFile.getParent()));
        if(srcFile.exists()){
            copyFile(srcFile, tmpFile, encoding);
        }
        return tmpFile;
    }

}
